package com.wson.widget.render.compass.framework;

import com.wson.behavior.element.IClickable;
import com.wson.behavior.element.ILabel;
import com.wson.behavior.element.ITabBody;
import com.wson.behavior.element.IToolElement;
import com.wson.behavior.layout.ITabs;
import com.wson.behavior.layout.IWindow;
import com.wson.behavior.property.IAppearance;
import com.wson.behavior.property.IPosition;
import com.wson.constant.compass.IAppActionConstant;
import com.wson.constant.compass.IRuntimeConstant;
import com.wson.information.message.constant.IMessageConstant;
import com.wson.model.basic.IBasicView;
import com.wson.model.common.group.impl.GroupModel;
import com.wson.model.compass.portfolio.ICompassPortfolioView;
import com.wson.platform.factory.impl.BeanFactory;
import com.wson.root.constant.IRootGraphicConstant;
import com.wson.root.util.LoggerUtil;
import com.wson.util.function.JScriptUtil;
import com.wson.util.tool.GraphicUtil;
import com.wson.widget.render.framework.impl.PortfolioCenterRender;
import com.wson.widget.render.framework.sterotype.AbstractWorkCenterRender;

public class AppWorkCenterRender extends AbstractWorkCenterRender {

	public AppWorkCenterRender() {
		super();
	}

	@Override
	public String code() {
		return RENDERCODE_WORKCENTER; 
	}
	
	//private boolean isCSSAvailable;

	@Override
	protected ITabs generateTabs() {
		
		
		//((ICompassPortfolioManip)this.getSessionProfile().getPortfolio()).setCodiskAccountID(1000L);
		
		ITabs tabs = super.generateTabs();
		tabs.addInternalParam(ITabs.PARAM_LEADICONSELECTED, GraphicUtil.getRootImageHref(tabs.getSystemProfile(), IRootGraphicConstant.ICONLINK_STARMARKBLUEFRAME));
		
		tabs.getHeaderPosition().setHalign(IAppearance.HALIGN_RIGHT);

		
		tabs.setToolbar(BeanFactory.getCurrentFactory().getBehaviorFactory().getToolbar());
		tabs.getToolbar().getMakeup().setColumnSpan(10);
		tabs.getToolbar().getAppearance().setHalign(IAppearance.HALIGN_RIGHT);
		
		tabs.getHeaderPosition().setHalign(IPosition.HALIGN_LEFT);
		
		ILabel welcome = BeanFactory.getCurrentFactory().getBehaviorFactory().getLabel();
		this.initBehavior(welcome);
		welcome.getAppearance().setType(ILabel.APPEARANCETYPE_CAPTION);
		welcome.getContent().setTitle("Welcome, "+this.getSessionProfile().getUser().getName());
		tabs.getToolbar().addFunctionElement(welcome);
		
		try{
			tabs.getToolbar().addFunctionElement( 
					this.generateCssFunction()
				);
			
			tabs.getToolbar().addFunctionElement( 
					generatePortfolioFunction()
				);
		}
		catch(Exception ex){
			LoggerUtil.DEBUG(ex);
		}

		
		tabs.getToolbar().addFunctionElement( 
				this.generateLogoffFunction(IAppActionConstant.ACTION_LOGOFF) 
			);

		
		GroupModel timeline = new GroupModel();
		BeanFactory.getCurrentFactory().getModelFactory().initModel((IBasicView)timeline);
		timeline.setID(1L);
		timeline.setShortText(this.getMessage(IRuntimeConstant.SYNC_TIMELINE));
		tabs.addPage(this.generateTabHeader(timeline, true), 
				this.generateTabBody(timeline, true), true);
		
		
		return tabs;
	}
	


	protected  IToolElement generateCssFunction()throws Exception{

		boolean isCSSAvailable = ((ICompassPortfolioView)this.getSessionProfile().getPortfolio()).getCodiskAccountID()!=null;
		if(!isCSSAvailable){
			IClickable action = this.generateLayoutPopupAction(
					this.initRender(new CodiskProvidersCenterRender()), 
					this.getMessage(IRuntimeConstant.SYNC_ADDCSS));
			action.getAppearance().setType(IClickable.APPEARANCETYPE_LINK);
			action.getNavigation().setHref(JScriptUtil.generateScriptVoid());
			return action;
		}
		else{
/*			CSSAccountFunc func=new CSSAccountFunc();
			this.initFunc(func);
			ICodiskAccountView account = func.function(
					this.getSessionProfile().getIdentity().getID());*/
			CodiskAccountRender render = new CodiskAccountRender();
			this.initRender(render);
			
			IClickable action = this.generateLayoutPopupAction(
					render, 
					this.getMessage(IRuntimeConstant.SYNC_CSSOK));
			action.getAppearance().setType(IClickable.APPEARANCETYPE_LINK);
			action.getNavigation().setHref(JScriptUtil.generateScriptVoid());
			return action;
		}
	}
	
	protected  IToolElement generatePortfolioFunction()throws Exception{
		PortfolioCenterRender render = new PortfolioCenterRender();
		this.initRender(render);
		render.setModel(this.getSessionProfile().getPortfolio());
		IClickable action = this.generateLayoutPopupAction(
				render, 
				this.getMessage(IMessageConstant.PORTFOLIOCENTER));
		action.getAppearance().setType(IClickable.APPEARANCETYPE_LINK);
		action.getNavigation().setHref(JScriptUtil.generateScriptVoid());
		return action;
	}
	
	protected  ITabBody generateTabBody(GroupModel model, boolean selected){

		
		IWindow win = BeanFactory.getCurrentFactory().getBehaviorFactory().getWindow();
		win.getAppearance().setFramed(true);
		win.getAppearance().setIndependent(false);
		win.getAppearance().setWidth("100%");
		
		
		FeedsRender trender = new FeedsRender();
		this.initRender(trender);
		
		
		
		win.getLoader().setBaseHref(trender.getReloadURL());

		
		/*ITable table = BeanFactory.getCurrentFactory().getBehaviorFactory().getTable();
		table.getAppearance().setType(ITable.APPEARANCETYPE_FEEDTABLE);
		this.initBehavior(table);
		//win.addPage(table);
		
		win.setLoadFunction(table.getLoadFunction());*/
		
		return win;
	}
}
